<?php
class PsController extends AppController {

	var $name = 'Ps';
	var $uses=array('Chiphi','Maup','Cpmaup','Zphieu','P','Kq',
		'Zphieukq','Zchiphicha','Ctrinh','Hangmuc','Tien',
		'Cgtrinhthuyetminh','Thuyetminh','Quyetdinh',
		'Ctrinhbb','Bb','ZpMucchaKp','ZpMucchaBc',
		//'Nguonvon',
		'Tien','Bc',
		'Maucung','Maucungct'
		);
	var $helpers=array('ctable');
	var $components=array('Nhoi','Cookie');

	function index($idbc) {

		$this->Cookie->write('idbc',$idbc);
		$idct=$this->Cookie->read('idct');

		$this->Bc->recursive = -1;
		$bc=$this->Bc->find('first',array(
			'conditions'=>array('id'=>$idbc),'fields'=>array('tenbaocao')));
		$this->Ctrinh->recursive = -1;
		$ct=$this->Ctrinh->find('first',array(
			'fields'=>array('id','tenctrinh'),'conditions'=>array('id'=>$idct)));
		
		$this->P->recursive = 0;
		$this->set('ps', $this->paginate('P',array('P.ctrinh_id'=>$idct,'P.bc_id'=>$idbc)));
		$this->set(compact('idct','ct','bc'));
	}

	function baocao($idbc){
//thong tin cong trinh
		$idct=$this->Cookie->read('idct');
		$this->Ctrinh->recursive = 0;
		$ct=$this->Ctrinh->find('first',array(
			//'fields'=>array('tenctrinh','Chudaut.tenchudautu'),
			'conditions'=>array('Ctrinh.id'=>$idct)));

//du lieu cac can cu
		$bb_daco=$this->Ctrinhbb->find('list',array(
			'conditions'=>array('ctrinh_id'=>$idct)));
		$this->Bb->recursive = -1;
		$bbs=$this->Bb->find('list', 
			array(
				'conditions'=>array('id'=>$bb_daco),
				'fields'=>array('id','tenbb')));
//du lieu cac quyet dinh
		$this->Quyetdinh->recursive = -1;
		$quyetdinhs=$this->Quyetdinh->find('list',array(
			'conditions'=>array('ctrinh_id'=>$idct),
			'fields'=>array('id','noidung')));	



//tien cac nguon von
		$this->Tien->recursive = 0;
		$tiens=$this->Tien->find('all',array(
			'conditions'=>array('ctrinh_id'=>$idct)));

		
//lay du lieu tong hop tu muc cha, du toan DE NGHI QUYET TOAN
		$this->ZpMucchaKp->recursive = -1;
		$phieutien_dukien=$this->ZpMucchaKp->find('all',array(
			'conditions'=>array('ctrinh_id'=>$idct,'noidung_id'=>3),
			'fields'=>array('tencp','sum(tien) as tongtien'),
			'group'=>array('tencp')));



//lay du lieu tong hop tu muc cha, du toan duoc duyet
		$this->ZpMucchaKp->recursive = -1;
		$phieutien_dcduyet=$this->ZpMucchaKp->find('all',array(
			'conditions'=>array('ctrinh_id'=>$idct,'noidung_id'=>2),
			'fields'=>array('tencp','sum(tien) as tongtien'),
			'group'=>array('tencp')));


//lay du lieu tong hop tu muc cac hang muc (de nghi quyet toan)
		$this->ZpMucchaBc->recursive = -1;
		$dulieuth=$this->ZpMucchaBc->find('all',array(
			'conditions'=>array('noidung_id'=>3,'bc_id'=>$idbc),
			'fields'=>array('tencp','sum(tien) as tongtien'),
			'group'=>array('tencp')));

		
		$this->set(compact('p','phieus','idp','ketqua',
			'ct','bbs','quyetdinhs',
			'phieutien_dcduyet','tong','tiens','phieutien_dukien','dulieuth'));	

		$this->layout='';
	}


/*

	function baocao($idp){
//thong tin cong trinh
		$idct=$this->Cookie->read('idct');
		$this->Ctrinh->recursive = 0;
		$ct=$this->Ctrinh->find('first',array(
			//'fields'=>array('tenctrinh','Chudaut.tenchudautu'),
			'conditions'=>array('Ctrinh.id'=>$idct)));

//du lieu cua phieu
		$this->P->recursive = 0;
		$p=$this->P->find('first',array('conditions'=>array('P.id'=>$idp)));

		$this->Kq->recursive = -1;		
		$ketqua=$this->Kq->find('list',array(
			'conditions'=>array('p_id'=>$idp),'fields'=>array('id','sotien')));
		$ketqua=array_values($ketqua);

		$this->Zphieu->recursive = -1;
		$phieus=$this->Zphieu->find('all',array(
			'conditions'=>array('maup_id'=>$p['P']['maup_id']),
			'fields'=>array('id','tencp','conhap','nhoi'),
			'order'=>array('nhoi'=>'asc')));

//lay du lieu tong hop tu muc cha, o1
		$tong=array();
		for($i=0;$i<=7;$i++){
			$this->ZpMucchaKp->recursive = -1;
			$t=$this->ZpMucchaKp->find('first',array(
				'conditions'=>array('p_id'=>$idp,'noidung_id'=>$i),
				'fields'=>array('sum(tien) as tongtien')));
			$tong[$i]=$t [0] ['tongtien'];
		}

	

//du lieu cac can cu
		$bb_daco=$this->Ctrinhbb->find('list',array(
			'conditions'=>array('ctrinh_id'=>$idct)));
		$this->Bb->recursive = -1;
		$bbs=$this->Bb->find('list', 
			array(
				'conditions'=>array('id'=>$bb_daco),
				'fields'=>array('id','tenbb')));
//du lieu cac quyet dinh
		$this->Quyetdinh->recursive = -1;
		$quyetdinhs=$this->Quyetdinh->find('list',array(
			'conditions'=>array('ctrinh_id'=>$idct),
			'fields'=>array('id','noidung')));	


//lay du lieu tong hop tu muc cha, du toan DE NGHI QUYET TOAN
		$this->ZpMucchaKp->recursive = -1;
		$phieutien_dukien=$this->ZpMucchaKp->find('all',array(
			'conditions'=>array('ctrinh_id'=>$idct,'noidung_id'=>3),
			'fields'=>array('tencp','sum(tien) as tongtien'),
			'group'=>array('tencp')));



//lay du lieu tong hop tu muc cha, du toan duoc duyet
		$this->ZpMucchaKp->recursive = -1;
		$phieutien_dcduyet=$this->ZpMucchaKp->find('all',array(
			'conditions'=>array('ctrinh_id'=>$idct,'noidung_id'=>2),
			'fields'=>array('tencp','sum(tien) as tongtien'),
			'group'=>array('tencp')));


//tien cac nguon von
		$this->Tien->recursive = 0;
		$tiens=$this->Tien->find('all',array(
			'conditions'=>array('ctrinh_id'=>$idct)));

		$this->set(compact('p','phieus','idp','ketqua',
			'ct','bbs','quyetdinhs',
			'phieutien_dcduyet','tong','tiens','phieutien_dukien'));	

		$this->layout='';
	}

	*/
	function quyetdinh($idbc){
		$this->layout='';
		$idct=$this->Cookie->read('idct');
		$this->Ctrinh->recursive = 0;
		$ct=$this->Ctrinh->find('first',array(
			//'fields'=>array('tenctrinh','Chudaut.tenchudautu'),
			'conditions'=>array('Ctrinh.id'=>$idct)));


		//du lieu cac quyet dinh
		$this->Quyetdinh->recursive = -1;
		$quyetdinhs=$this->Quyetdinh->find('list',array(
			'conditions'=>array('ctrinh_id'=>$idct),
			'fields'=>array('id','noidung')));	



//tien cac nguon von
		$this->Tien->recursive = 0;
		$tiens=$this->Tien->find('all',array(
			'conditions'=>array('ctrinh_id'=>$idct)));


//lay du lieu tong hop tu muc cha, du toan DE NGHI QUYET TOAN
		$this->ZpMucchaKp->recursive = -1;
		$phieutien_dukien=$this->ZpMucchaKp->find('all',array(
			'conditions'=>array('ctrinh_id'=>$idct,'noidung_id'=>3),
			'fields'=>array('tencp','sum(tien) as tongtien'),
			'group'=>array('tencp')));



//lay du lieu tong hop tu muc cha, du toan duoc duyet
		$this->ZpMucchaKp->recursive = -1;
		$phieutien_dcduyet=$this->ZpMucchaKp->find('all',array(
			'conditions'=>array('ctrinh_id'=>$idct,'noidung_id'=>2),
			'fields'=>array('tencp','sum(tien) as tongtien'),
			'group'=>array('tencp')));

	//	print_r($phieutien_dcduyet);
		$this->set(compact('ct','quyetdinhs','tiens',
			'phieutien_dcduyet','phieutien_dukien'));
	}

	function biahoso_thkp($idp){
		


		//thong tin cong trinh
		$idct=$this->Cookie->read('idct');
		$this->Ctrinh->recursive = 0;
		$ct=$this->Ctrinh->find('first',array(
			//'fields'=>array('tenctrinh','Chudaut.tenchudautu'),
			'conditions'=>array('Ctrinh.id'=>$idct)));

		//du lieu cua phieu
		$this->P->recursive = 0;
		$p=$this->P->find('first',array('conditions'=>array('P.id'=>$idp)));

		//du lieu cac can cu
		$bb_daco=$this->Ctrinhbb->find('list',array(
			'conditions'=>array('ctrinh_id'=>$idct)));
		$this->Bb->recursive = -1;
		$bbs=$this->Bb->find('list', 
			array(
				'conditions'=>array('id'=>$bb_daco),
				'fields'=>array('id','tenbb')));
		$this->set(compact('bbs','ct','p'));
		$this->layout='';
	}

	function thkinhphi($idp){

		$this->Ctrinh->recursive = 0;
		$ct=$this->Ctrinh->find('first',array(
			//'fields'=>array('tenctrinh','Chudaut.tenchudautu'),
			'conditions'=>array('Ctrinh.id'=>$this->Cookie->read('idct'))));




		$this->P->recursive = 0;
		$p=$this->P->find('first',array('conditions'=>array('P.id'=>$idp)));

		$this->Kq->recursive = -1;		
		$ketqua=$this->Kq->find('list',array(
			'conditions'=>array('p_id'=>$idp),'fields'=>array('id','sotien')));
		$ketqua=array_values($ketqua);

		$this->Zphieu->recursive = -1;
		$phieus=$this->Zphieu->find('all',array(
			'conditions'=>array('maup_id'=>$p['P']['maup_id']),
			'fields'=>array('id','tencp','conhap','nhoi'),
			'order'=>array('nhoi'=>'asc')));

//lay du lieu tong hop tu muc cha, o1
		$tong=array();
		for($i=0;$i<=7;$i++){
			$this->ZpMucchaKp->recursive = -1;
			$t=$this->ZpMucchaKp->find('first',array(
				'conditions'=>array('p_id'=>$idp,'noidung_id'=>$i),
				'fields'=>array('sum(tien) as tongtien')));
			$tong[$i]=$t [0] ['tongtien'];
		}

		
		$this->set(compact('p','phieus','idp','ketqua','ct',
			'phieutien_dcduyet',
			'tong'));	
		$this->layout='';
	}
	function thuyetminh_thkp($idp){
		//thong tin cong trinh
		$idct=$this->Cookie->read('idct');
		$this->Ctrinh->recursive = 0;
		$ct=$this->Ctrinh->find('first',array(
			//'fields'=>array('tenctrinh','Chudaut.tenchudautu'),
			'conditions'=>array('Ctrinh.id'=>$idct)));

//du lieu cua phieu
		$this->P->recursive = 0;
		$p=$this->P->find('first',array('conditions'=>array('P.id'=>$idp)));

		$this->Kq->recursive = -1;		
		$ketqua=$this->Kq->find('list',array(
			'conditions'=>array('p_id'=>$idp),'fields'=>array('id','sotien')));
		$ketqua=array_values($ketqua);

		$this->Zphieu->recursive = -1;
		$phieus=$this->Zphieu->find('all',array(
			'conditions'=>array('maup_id'=>$p['P']['maup_id']),
			'fields'=>array('id','tencp','conhap','nhoi'),
			'order'=>array('nhoi'=>'asc')));

	

//du lieu cac can cu
		$bb_daco=$this->Ctrinhbb->find('list',array(
			'conditions'=>array('ctrinh_id'=>$idct)));
		$this->Bb->recursive = -1;
		$bbs=$this->Bb->find('list', 
			array(
				'conditions'=>array('id'=>$bb_daco),
				'fields'=>array('id','tenbb')));
//du lieu cac quyet dinh
		$this->Quyetdinh->recursive = -1;
		$quyetdinhs=$this->Quyetdinh->find('list',array(
			'conditions'=>array('ctrinh_id'=>$idct),
			'fields'=>array('id','noidung')));	

//du lieu cac thuyet minh
		$tm=$this->Cgtrinhthuyetminh->find('list',array(
			'conditions'=>array('ctrinh_id'=>$idct),
			'fields'=>array('thuyetminh_id','thuyetminh_id')
			));
		//print_r($bb_daco);
		$this->Thuyetminh->recursive = -1;
		$this->set('thuyetminhs', $this->Thuyetminh->find('list',
			array('conditions'=>array('id'=>$bb_daco),
				'fields'=>array('id','digiaitm'))));




//lay du lieu tong hop tu muc cha, du toan duoc duyet
		//MUC A1- VE VIEC PHE DUYET HO SO
		$this->ZpMucchaKp->recursive = -1;
		$phieutien_dcduyet=$this->ZpMucchaKp->find('all',array(
			'conditions'=>array('p_id'=>$idp,'noidung_id'=>2)));
		$tong_cp=$this->ZpMucchaKp->find('first',array(
			'fields'=>array('p_id','noidung_id','sum(tien) as tongtien'),
			'group'=>array('p_id','noidung_id'),
			'conditions'=>array('p_id'=>$idp,'noidung_id'=>2)));


//lay du lieu tong hop tu muc cha, du toan duoc duyet
		//MUC A2- VE VIEC QUYET TOAN HO SO
		$this->ZpMucchaKp->recursive = -1;
		$phieutien_dcduyet_a2=$this->ZpMucchaKp->find('all',array(
			'conditions'=>array('p_id'=>$idp,'noidung_id'=>3)));
		$tong_cp_a2=$this->ZpMucchaKp->find('first',array(
			'fields'=>array('p_id','noidung_id','sum(tien) as tongtien'),
			'group'=>array('p_id','noidung_id'),
			'conditions'=>array('p_id'=>$idp,'noidung_id'=>3)));

		
//print_r($tong_cp);	

		$this->set(compact('p','phieus','idp','ketqua',
			'ct','bbs','quyetdinhs',
			'phieutien_dcduyet','tong_cp',
			'phieutien_dcduyet_a2','tong_cp_a2'));	

		$this->layout='';
	}
	
	function xuatbc($idct,$idp) {
		if (!$idct) {
			$this->Session->setFlash(__('Invalid ctrinh', true));
			$this->redirect(array('action' => 'index'));
		}

	
		$this->Ctrinh->recursive = 0;
		$ct=$this->Ctrinh->find('first',array(
			//'fields'=>array('tenctrinh','Chudaut.tenchudautu'),
			'conditions'=>array('Ctrinh.id'=>$idct)));

//du lieu cua phieu
		$this->P->recursive = 0;
		$p=$this->P->find('first',array('conditions'=>array('P.id'=>$idp)));


		$this->set(compact('ct','idct','idp','p'));
		
	}
/*
	function xuatbc($idp) {

		$this->Ctrinh->recursive = 0;
		$ct=$this->Ctrinh->find('first',array(
			//'fields'=>array('tenctrinh','Chudaut.tenchudautu'),
			'conditions'=>array('Ctrinh.id'=>$idct)));



		$this->P->recursive = -1;
		$p=$this->P->find('first',array('conditions'=>array('id'=>$idp)));

		$this->Kq->recursive = -1;		
		$ketqua=$this->Kq->find('list',array(
			'conditions'=>array('p_id'=>$idp),'fields'=>array('id','sotien')));
		$ketqua=array_values($ketqua);

		$this->Zphieu->recursive = -1;
		$phieus=$this->Zphieu->find('all',array(
			'conditions'=>array('maup_id'=>$p['P']['maup_id']),
			'fields'=>array('id','tencp','conhap','nhoi'),
			'order'=>array('nhoi'=>'asc')));

		$this->set(compact('p','phieus','idp','ketqua','ct'));		
		
	}
	*/
	/* *
	xuat ra file word cho xuatbc o tren
	*/
	function word($idct) {
		if (!$idct) {
			$this->Session->setFlash(__('Invalid ctrinh', true));
			$this->redirect(array('action' => 'index'));
		}
		
		$this->layout='';
		$this->Ctrinh->recursive = 0;
		$ct=$this->Ctrinh->find('first',array(
			//'fields'=>array('tenctrinh','Chudaut.tenchudautu'),
			'conditions'=>array('Ctrinh.id'=>$idct)));

		$this->Tien->recursive = 0;
		$this->set('tiens', $this->Tien->find('all',array('conditions'=>array('ctrinh_id'=>$idct))));
	
		$this->Cgtrinhthuyetminh->recursive = -1;
		$tm_daco=$this->Cgtrinhthuyetminh->find('list',array(
			'fields'=>array('thuyetminh_id','thuyetminh_id'),
			'conditions'=>array('ctrinh_id'=>$idct)));

		$this->Thuyetminh->recursive = -1;
		$this->set('thuyetminhs', 
			$this->Thuyetminh->find('all',array('conditions'=>array('id'=>$tm_daco))));	


		$this->Quyetdinh->recursive = -1;
		$this->set('quyetdinhs', $this->paginate('Quyetdinh',array('ctrinh_id'=>$idct)));


		$this->set(compact('ct','idct'));
		
	}

	function khoitao($idct){
		$idbc=$this->Cookie->read('idbc');

	//tao mau moi
		$this->P->query('insert into maups(ngaytao) values (CURRENT_TIMESTAMP( ))');		
	//lay id mau vua tao
		$this->Maup->recursive = -1;
		$idmau=$this->Maup->find('first',array('order'=>array('id'=>'desc')));		
		$idmau=$idmau['Maup']['id'];

	//them vao phieu voi mau vua tao
		$this->P->query('insert into ps(tenphieu,ctrinh_id,maup_id,bc_id) 
				values ("Tên phieu",'.$idct.','.$idmau.','.$idbc.')');		
	//lay id cua phieu moi tao
		$this->P->recursive = -1;
		$idp=$this->P->find('first',array('order'=>array('id'=>'desc')));		
		$idp=$idp['P']['id'];	
	

##	//lay 10 chi phi "cung", luon co trong cac phieu
##		$this->Chiphi->recursive = -1;
##		$chiphis = $this->Chiphi->find('list',
##			array(
##					'conditions'=>array('id between ? and ?'=>array(0,10))));
##	//chay luu 10 chi phi "cung"

		$this->Maucung->recursive = -1;
		$idmaucung = $this->Maucung->find('first',array('fields'=>array('id')));
		$this->Maucungct->recursive = -1;
		$chiphis = $this->Maucungct->find('list',
			array('conditions'=>
					array('maucung_id'=>$idmaucung ['Maucung'] ['id']),
					'fields'=>array('chiphi_id','chiphi_id')));

		foreach ($chiphis as $k) {
			$this->Cpmaup->create();
			$this->data['Cpmaup']['maup_id']=$idmau;
			$this->data['Cpmaup']['chiphi_id']=$k;
			$this->data['Cpmaup']['conhap']=1;
		
			$this->Cpmaup->save($this->data);
		}
	//khi hoan tat luu, chuyen qua trang "taomau"
		$this->redirect(array('action' => 'taomau',$idct,$idmau,$idp));
				

	}
	/* *
	Trang nay khi da co 1 phieu duoc tao san, khi bat trang ra o che do edit phieu
	*/
	function taomau($idct,$idmau,$idp=null) {
		$this->set(compact('idct'));
		if(empty($idct)){
			$this->Session->setFlash(__('Công trình không hợp lệ!!', true));
			$this->redirect(array('controller'=>'ctrinhs','action' => 'index'));
		}else	
		if (!empty($this->data)) {
			if ($this->P->query('update ps set 
					tenphieu="'.$this->data['P']['tenphieu'].'" 
					where id='.$idp)) {
				$this->Session->setFlash(__('Mẫu phiếu nhập đã tạo xong!!', true));
				$this->redirect(array('action' => 'index',$idct));
			} else {
				$this->Session->setFlash(__('The p could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->P->read(null, $idp);
		}

		$this->Hangmuc->recursive=-1;
		$hangmucs=$this->Hangmuc->find('list',array(
			'fields'=>array('id','tenhm'),'conditions'=>array('ctrinh_id'=>$idct)));
	

		$this->Zphieu->recursive = -1;

		$ttphieu=$this->Zphieu->find('all',array(
			'conditions'=>array('maup_id'=>$idmau),
			'fields'=>array('id','tencp','nhoi'),
			'order'=>array('nhoi'=>'asc')));

		$this->set(compact('idmau','ttphieu','idp','idct','hangmucs'));
	}
	/***
	Trang nay de them (moi) cac chi phi vao phieu
	Them duoc ca trong truong hop phieu da nhap du lieu
	*/
	function themcp($idmau,$idct,$idp){

	//neu co nhap lieu
		if (!empty($this->data)) {
		//khi bam nut them se tao chi phi moi ben bang chi phi..
			//1 vai truong can xet: luc moi tao ra conhap=1, 
			//IF "có con" THEN "nut goc:=conhap=0"
			$data_idcha=$this->data['P']['chiphi_id'];

			$this->P->query('insert into chiphis
					(tencp,chiphi_id) values 
						("'.$this->data['P']['tencp'].'",
							"'.$data_idcha.'")');		
			
			if($data_idcha!=-1)
				$conhap=1;
			else
				$conhap=0;

		//..sau do lay id vua moi tao..
			$this->Chiphi->recursive = -1;
			$id_chiphiall=$this->Chiphi->find('first',
				array('order'=>array('id'=>'desc'),'fields'=>array('id','chiphi_id')));		
			$id_chiphi=$id_chiphiall['Chiphi']['id'];

		//nhoi so de co do dai bang nhau (cho viec sap xep)
			$this->Chiphi->recursive = -1;
			$chacp=$this->Chiphi->find('first',
				array('fields'=>array('nhoi'),'conditions'=>
					array('id'=>$id_chiphiall['Chiphi']['chiphi_id'])));

			$nhoi=$chacp['Chiphi']['nhoi'].$this->Nhoi->nhoichuoi($id_chiphi);
			$this->P->query('update chiphis set nhoi="'.$nhoi.'"
					where id='.$id_chiphi);	

			
		//.. de them vao cpmaup cap nhat lai cac chi phi co trong mau
			if ($this->Cpmaup->query('insert into cpmaups (maup_id,chiphi_id,conhap)
					values ('.$idmau.','.$id_chiphi.','.$conhap.')')) {
					
					$this->Cpmaup->query('update cpmaups set conhap=0
						where maup_id='.$idmau.' and chiphi_id='.$data_idcha);

					$this->Kq->recursive=-1;
					$kqs=$this->Kq->find('first',array(
						'fields'=>array('id'),'conditions'=>array('p_id'=>$idp)));
				//neu da co du lieu da nhap phieu thi cap nhat cac chi phi moi =0
					if(!empty($kqs)){
						//chay het 8  noi dung
						for($m=0;$m<8;$m++)
							$this->Cpmaup->query('insert into kqs (p_id,chiphi_id,noidung_id,sotien)
					values ('.$idp.','.$id_chiphi.','.$m.',0)');
						//cap nhat so lieu cho chi phi cha
					$this->Kq->query('
						update kqs, zchiphichas set sotien=st 
							where zchiphichas.chiphi_cha=kqs.chiphi_id and kqs.noidung_id=zchiphichas.noidung_id
							and kqs.p_id='.$idp);
					}
					$this->Session->setFlash(__('Chi phí đã được thêm', true));
					$this->redirect(array('action' => 'themcp',$idmau,$idct,$idp));
			} else {
				$this->Session->setFlash(__('Bị lỗi khi thêm chi phí vào mẫu phiếu, Anh (chị) vui lòng thử lại', true));
			}
		} 

	//lay danh sach cac phieu cha (goc)	

		//lay cac chi phi da co	
		$cp_da_co=$this->Cpmaup->find('list',array(
			'conditions'=>array('maup_id'=>$idmau),
			'fields'=>array('chiphi_id','chiphi_id')));
		
		$this->Chiphi->recursive = -1;
		$id_cpcha=$this->Chiphi->find('list',array(
			'fields'=>array('id','tencp'),
			'conditions'=>array('chiphi_id'=>-1,'id'=>$cp_da_co)));

	//lay cac phieu cha cua mau phieu
		$this->Chiphi->recursive = 1;
		$cpcha=$this->Chiphi->query(' select tencp 
				from chiphis Chiphi, cpmaups b where Chiphi.id=b.chiphi_id 
				and maup_id="'.$idmau.'" and Chiphi.chiphi_id=-1');

//		print_r($cpcha);
	//lay cac cp cua phieu
		$this->Zphieu->recursive = -1;
	
		$ttphieu=$this->Zphieu->find('all',array(
			'conditions'=>array('maup_id'=>$idmau),
			'fields'=>array('id','tencp','nhoi'),
			'order'=>array('nhoi'=>'asc')));

		$this->set(compact('idmau','ttphieu','id_cpcha','cpcha','idct','idp'));
	}
	/**
	*Phan nay de sua thong tin cua phieu (neu phieu da co thong tin)
	*/
	function sua_nhaplieu($idp){
	//lay thong tin phieu
//		print_r('so dong moi:'.count($this->data));
		$this->P->recursive = -1;
		$p=$this->P->find('first',array('conditions'=>array('id'=>$idp)));

		if(!empty($this->data)){
			$this->Zphieu->recursive = -1;
		
			$pt=$this->Zphieu->find('list',array(
				'conditions'=>array('maup_id'=>$p['P']['maup_id']),
			
				'order'=>array('nhoi'=>'asc')));
			//sap xep lai id cua mang cho chay thu tu tang dan
			$pt=array_values($pt);

			$so_dl=8*count($pt);
			//print_r('so dong chay '.$so_dl);
			for( $i=0;$i<$so_dl;$i++) {
				if(!$this->Kq->query('update kqs set sotien="'.$this->data['P'][$i].'"
						where p_id="'.$idp.'" and chiphi_id="'.$pt[floor($i/8)].'" and
							noidung_id="'.($i%8).'"'))
				{
					$this->Session->setFlash(__('Phiếu lưu chưa thành công, Anh (chị) vui lòng thử lại', true));
					$this->redirect(array('action' => 'index',$p['P']['ctrinh_id']));
				}
	
			}
			//cap nhat so lieu cho chi phi cha
			$this->Kq->query('
				update kqs, zchiphichas set sotien=st 
					where zchiphichas.chiphi_cha=kqs.chiphi_id and kqs.noidung_id=zchiphichas.noidung_id
					and kqs.p_id='.$idp);


			$this->Session->setFlash(__('Lưu thành công!!', true));
			$this->redirect(array('action' => 'index',$p['P']['ctrinh_id']));
		}		
	//lay cac cp cua phieu
		$this->Zphieu->recursive = -1;
		$phieus=$this->Zphieu->find('all',array(
			'conditions'=>array('maup_id'=>$p['P']['maup_id']),
			'fields'=>array('id','tencp','conhap','nhoi'),
			'order'=>array('nhoi'=>'asc')));
//print_r($phieus);

	//Lay du lieu ket qua de do vo o nhap lieu
		$this->Kq->recursive = -1;		
		$ketqua=$this->Kq->find('list',array(
			'conditions'=>array('p_id'=>$idp),'fields'=>array('id','sotien')));
		$ketqua=array_values($ketqua);
//print_r($ketqua);
		$this->set(compact('p','phieus','pt','idp','ketqua'));

	}
	
	function nhaplieu($idp){
	//lay thong tin phieu
//		print_r('so dong moi:'.count($this->data));
		
//lay du lieu kiem tra thong, xem phieu da duoc nhap chua
		$k=$this->Kq->find('first',array(
			'fields'=>array('id'),'conditions'=>array('p_id'=>$idp)));

//neu da dc nhap chuyen qua phan sua thong tin
		if(!empty($k))
			$this->redirect(array('action' => 'sua_nhaplieu',$idp));

		$this->P->recursive = -1;
		$p=$this->P->find('first',array('conditions'=>array('id'=>$idp)));

		if(!empty($this->data)){
			$this->Zphieu->recursive = -1;
		
			$pt=$this->Zphieu->find('list',array(
				'conditions'=>array('maup_id'=>$p['P']['maup_id']),
			
				'order'=>array('nhoi'=>'asc')));
			//sap xep lai id cua mang cho chay thu tu tang dan
			$pt=array_values($pt);

			$so_dl=8*count($pt);
			//print_r('so dong chay '.$so_dl);
			for( $i=0;$i<$so_dl;$i++) {
				if(!$this->Kq->query('insert into kqs (
							p_id, 
							chiphi_id, 
							noidung_id, 
							sotien)
						values("'.$idp.'","'.
							$pt[floor($i/8)].'",
							"'.($i%8).'",
							"'.$this->data['P'][$i].'")')){
					$this->Session->setFlash(__('Phiếu lưu chưa thành công, Anh (chị) vui lòng thử lại', true));
					$this->redirect(array('action' => 'index',$p['P']['ctrinh_id']));
				}
	
			}
			//cap nhat so lieu cho chi phi cha
			$this->Kq->query('
				update kqs, zchiphichas set sotien=st 
					where zchiphichas.chiphi_cha=kqs.chiphi_id and kqs.noidung_id=zchiphichas.noidung_id
					and kqs.p_id='.$idp);


			$this->Session->setFlash(__('Lưu thành công!!', true));
			$this->redirect(array('action' => 'index',$p['P']['ctrinh_id']));
		}		
	//lay cac cp cua phieu
		$this->Zphieu->recursive = -1;
		$phieus=$this->Zphieu->find('all',array(
			'conditions'=>array('maup_id'=>$p['P']['maup_id']),
			'fields'=>array('id','tencp','conhap','nhoi'),
			'order'=>array('nhoi'=>'asc')));

		$this->set(compact('p','phieus','pt','idp'));

	}

	function xuat($idp){
		$this->P->recursive = -1;
		$p=$this->P->find('first',array('conditions'=>array('id'=>$idp)));

		$this->Kq->recursive = -1;		
		$ketqua=$this->Kq->find('list',array(
			'conditions'=>array('p_id'=>$idp),'fields'=>array('id','sotien')));
		$ketqua=array_values($ketqua);

		$this->Zphieu->recursive = -1;
		$phieus=$this->Zphieu->find('all',array(
			'conditions'=>array('maup_id'=>$p['P']['maup_id']),
			'fields'=>array('id','tencp','conhap','nhoi'),
			'order'=>array('nhoi'=>'asc')));

		$this->set(compact('p','phieus','idp','ketqua'));		
	}
	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid p', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->P->save($this->data)) {
				$this->Session->setFlash(__('The p has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The p could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->P->read(null, $id);
		}
		$ctrinhs = $this->P->Ctrinh->find('list');
		$maups = $this->P->Maup->find('list');
		$this->set(compact('ctrinhs', 'maups'));
	}

	function delete($id = null) {
		$idbc=$this->Cookie->read('idbc');
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for p', true));
			$this->redirect(array('action'=>'index',$idbc));
		}
		if ($this->P->delete($id)) {
			$this->Session->setFlash(__('P deleted', true));
			$this->redirect(array('action'=>'index',$idbc));
		}
		$this->Session->setFlash(__('P was not deleted', true));
		$this->redirect(array('action' => 'index',$idbc));
	}

	function bc($idbc)
	{
		
		$this->P->recursive = -1;
		$ps_id=$this->P->find('list',array('fields'=>array('id','id'),
			'conditions'=>array('bc_id'=>$idbc)));
		
		$ketqua=array();
		$phieus=array();
		$tenphieu=array();
		$i=0;
		foreach ($ps_id as $idp) {
			$this->P->recursive = -1;
			$p=$this->P->find('first',array('conditions'=>array('id'=>$idp)));
			$tenphieu [$i]=$p ['P'] ['tenphieu'];
			$this->Kq->recursive = -1;		
			$ketqua[$i]=$this->Kq->find('list',array(
				'conditions'=>array('p_id'=>$idp),'fields'=>array('id','sotien')));
			$ketqua [$i]=array_values($ketqua[$i]);

			$this->Zphieu->recursive = -1;
			$phieus [$i]=$this->Zphieu->find('all',array(
				'conditions'=>array('maup_id'=>$p['P']['maup_id']),
				'fields'=>array('id','tencp','conhap','nhoi'),
				'order'=>array('nhoi'=>'asc')));
			$i++;
	
		}
		
		$this->set(compact('p','phieus','idp','ketqua','tenphieu','idbc'));	
	}


	function bcthamtra($idbc)
	{
		
		$this->P->recursive = -1;
		$ps_id=$this->P->find('list',array('fields'=>array('id','id'),
			'conditions'=>array('bc_id'=>$idbc)));
		
		$ketqua=array();
		$phieus=array();
		$tenphieu=array();
		$i=0;
		foreach ($ps_id as $idp) {
			$this->P->recursive = -1;
			$p=$this->P->find('first',array('conditions'=>array('id'=>$idp)));
			$tenphieu [$i]=$p ['P'] ['tenphieu'];
			$this->Kq->recursive = -1;		
			$ketqua[$i]=$this->Kq->find('list',array(
				'conditions'=>array('p_id'=>$idp),'fields'=>array('id','sotien')));
			$ketqua [$i]=array_values($ketqua[$i]);

			$this->Zphieu->recursive = -1;
			$phieus [$i]=$this->Zphieu->find('all',array(
				'conditions'=>array('maup_id'=>$p['P']['maup_id']),
				'fields'=>array('id','tencp','conhap','nhoi'),
				'order'=>array('nhoi'=>'asc')));
			$i++;
	
		}
		
		$this->set(compact('p','phieus','idp','ketqua','tenphieu','idbc'));	
	}
	function bcxls($idbc)
	{
		
		$this->layout='';
		$this->P->recursive = -1;
		$ps_id=$this->P->find('list',array('fields'=>array('id','id'),
			'conditions'=>array('bc_id'=>$idbc)));
		
		$ketqua=array();
		$phieus=array();
		$tenphieu=array();
		$i=0;
		foreach ($ps_id as $idp) {
			$this->P->recursive = -1;
			$p=$this->P->find('first',array('conditions'=>array('id'=>$idp)));
			$tenphieu [$i]=$p ['P'] ['tenphieu'];
			$this->Kq->recursive = -1;		
			$ketqua[$i]=$this->Kq->find('list',array(
				'conditions'=>array('p_id'=>$idp),'fields'=>array('id','sotien')));
			$ketqua [$i]=array_values($ketqua[$i]);

			$this->Zphieu->recursive = -1;
			$phieus [$i]=$this->Zphieu->find('all',array(
				'conditions'=>array('maup_id'=>$p['P']['maup_id']),
				'fields'=>array('id','tencp','conhap','nhoi'),
				'order'=>array('nhoi'=>'asc')));
			$i++;
	
		}
		
		$this->set(compact('p','phieus','idp','ketqua','tenphieu','idbc'));	
	}


	function bcthamtraxls($idbc)
	{
		
		$this->layout='';
		$this->P->recursive = -1;
		$ps_id=$this->P->find('list',array('fields'=>array('id','id'),
			'conditions'=>array('bc_id'=>$idbc)));
		
		$ketqua=array();
		$phieus=array();
		$tenphieu=array();
		$i=0;
		foreach ($ps_id as $idp) {
			$this->P->recursive = -1;
			$p=$this->P->find('first',array('conditions'=>array('id'=>$idp)));
			$tenphieu [$i]=$p ['P'] ['tenphieu'];
			$this->Kq->recursive = -1;		
			$ketqua[$i]=$this->Kq->find('list',array(
				'conditions'=>array('p_id'=>$idp),'fields'=>array('id','sotien')));
			$ketqua [$i]=array_values($ketqua[$i]);

			$this->Zphieu->recursive = -1;
			$phieus [$i]=$this->Zphieu->find('all',array(
				'conditions'=>array('maup_id'=>$p['P']['maup_id']),
				'fields'=>array('id','tencp','conhap','nhoi'),
				'order'=>array('nhoi'=>'asc')));
			$i++;
	
		}
		
		$this->set(compact('p','phieus','idp','ketqua','tenphieu','idbc'));	
	}
	
}
